package com.example.droideep.common.view;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.example.droideep.R;

/**
 * Created by goodev on 15/5/4.
 */
public class ArcMenu extends LinearLayout implements View.OnClickListener {

    private boolean isOpen;

    public ArcMenu(Context context) {
        this(context, null);
    }

    public ArcMenu(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ArcMenu(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public ArcMenu(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init();
    }

    private void init() {
        setOrientation(VERTICAL);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);
        if (changed) {
            final int count = getChildCount();
            View child;
            for (int i = 0; i < count; i++) {
                child = getChildAt(i);
                child.setOnClickListener(this);
            }
        }
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.arc_menu) {
            toggle();
            Toast.makeText(getContext(), "ArcMenu", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(getContext(), "ArcItem", Toast.LENGTH_SHORT).show();
        }
    }

    private void toggle() {
        if (isOpen) {
            close();
            isOpen = false;
        } else {
            open();
            isOpen = true;
        }
    }

    private void open() {
        final int count = getChildCount();
        for (int i = 0; i < count; i++) {
            View child = getChildAt(i);
            if (child.getId() == R.id.arc_menu) {
                rotateMenu(child.findViewById(R.id.iv_add), 0f, 135f);
            } else {
                actionItem(child, true, 0);
            }
        }
    }

    private void close() {
        final int count = getChildCount();
        for (int i = 0; i < count; i++) {
            View child = getChildAt(i);
            if (child.getId() == R.id.arc_menu) {
                rotateMenu(child.findViewById(R.id.iv_add), 135f, 0f);
            } else {
                actionItem(child, false, 0);
            }
        }
    }

    private void rotateMenu(View v, float from, float to) {
        ObjectAnimator rotate = ObjectAnimator.ofFloat(v, "rotation", from, to);
        rotate.setDuration(200);
        rotate.start();
    }

    private void actionItem(View v, boolean isOpen, long delay) {
        v.setPivotX(v.getMeasuredWidth() / 2.0f);
        v.setPivotY(v.getMeasuredHeight());

        final int fromX = isOpen ? 0 : 1;
        final int toX = isOpen ? 1 : 0;
        final int fromY = isOpen ? 0 : 1;
        final int toY = isOpen ? 1 : 0;
        ObjectAnimator scaleX = ObjectAnimator.ofFloat(v, "scaleX", fromX, toX);
        ObjectAnimator scaleY = ObjectAnimator.ofFloat(v, "scaleY", fromY, toY);
        AnimatorSet scaleSet = new AnimatorSet();
        scaleSet.playTogether(scaleX, scaleY);
        scaleSet.setStartDelay(delay);
        scaleSet.setDuration(200);
        scaleSet.start();
    }
}
